package com.ruoyi.web.controller.saas;

import com.bonuo.saas.domain.IntellectualProperty;
import com.bonuo.saas.service.IntellectualPropertyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/saas/intellectualProperty")
@Api(tags = "知识产权管理")
public class IntellectualPropertyController extends BaseController {

    @Autowired
    private IntellectualPropertyService intellectualPropertyService;

    @ApiOperation(value = "查询知识产权信息",httpMethod = "GET")
    @GetMapping("/list")
    public AjaxResult list(IntellectualProperty intellectualProperty, Integer pageNum, Integer pageSize) {
        if(pageNum!=null && pageSize!=null){
            startPage();
        }
        List<IntellectualProperty> list = intellectualPropertyService.selectIntellectualPropertyList(intellectualProperty);
        //查询专利发明类型授权有效的数量
        Integer inventPatentCount = intellectualPropertyService.selectPatentCount(1);
        //查询专利实用新型类型授权有效的数量
        Integer utilityModelPatentCount = intellectualPropertyService.selectPatentCount(2);
        //查询专利外观设计类型授权有效的数量
        Integer appearanceDesignPatentCount = intellectualPropertyService.selectPatentCount(3);
        //查询软著已登记数量
        Integer copyrightCount = intellectualPropertyService.selectCopyrightCount();
        // 返回数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", getDataTable(list));
        map.put("inventPatentCount", inventPatentCount);
        map.put("utilityModelPatentCount", utilityModelPatentCount);
        map.put("appearanceDesignPatentCount", appearanceDesignPatentCount);
        map.put("copyrightCount", copyrightCount);

        return success(map);
    }

    @ApiOperation(value = "添加知识产权信息",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody IntellectualProperty intellectualProperty) {

        //先查询专利、登记号是否存在
        if(intellectualProperty.getRegistrationNumber() != null && intellectualProperty.getRegistrationNumber() != ""){
            String registrationNumber = intellectualPropertyService.selectIntellectualPropertyByRegistrationNumber(intellectualProperty);
            if(registrationNumber != null && !registrationNumber.equals("")){
                return error("该专利、登记号已存在");
            }
        }
        return toAjax(intellectualPropertyService.insertIntellectualProperty(intellectualProperty));
    }

    @ApiOperation(value = "修改知识产权信息",httpMethod = "PUT")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody IntellectualProperty intellectualProperty) {

        //先查询专利、登记号是否存在
        if(intellectualProperty.getRegistrationNumber() != null && intellectualProperty.getRegistrationNumber() != ""){
            String registrationNumber = intellectualPropertyService.selectIntellectualPropertyByRegistrationNumber(intellectualProperty);
            if(registrationNumber != null && !registrationNumber.equals("")){
                return error("该专利、登记号已存在");
            }
        }

        return toAjax(intellectualPropertyService.updateIntellectualProperty(intellectualProperty));
    }

    @ApiOperation(value = "删除知识产权信息",httpMethod = "DELETE")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Integer id) {
        return toAjax(intellectualPropertyService.deleteIntellectualPropertyById(id));
    }

    @ApiOperation(value = "导出知识产权信息",httpMethod = "POST")
    @PostMapping("/export")
    public void export(HttpServletResponse response, IntellectualProperty intellectualProperty) {

        List<IntellectualProperty> list = intellectualPropertyService.selectIntellectualPropertyList(intellectualProperty);

        ExcelUtil<IntellectualProperty> util = new ExcelUtil<IntellectualProperty>(IntellectualProperty.class);

        // 根据类型排除无关字段
        if (intellectualProperty.getType() == 1) { // 专利：排除商标、软著特有字段
            util.showColumn(
                    "patentType", "registrationNumber", "name","acquisitionMethod","rightHolder", "transferorUnit", "transferorDate", "applicationDate",
                    "status", "authorizationDate", "inventor", "correspondingDevice", "highNewUsage", "cooperativeAgency", "isAntecedentTrial",
                    "technologyPerson",  "remark"
            );
            util.exportExcel(response, list, "专利数据");
        } else if (intellectualProperty.getType() == 2) { // 软著：排除专利、商标特有字段
            util.showColumn(
                    "registrationNumber", "name",
                    "acquisitionMethod", "rightHolder", "transferorUnit", "transferorDate", "applicationDate", "status", "authorizationDate",
                    "correspondingDevice", "highNewUsage", "remark"
            );
            util.exportExcel(response, list, "软著数据");
        } else if (intellectualProperty.getType() == 3) { // 商标：排除专利、软著特有字段
            util.showColumn(
                    "trademarkContent", "internationalClassification", "registrant", "registrationDate", "validityDate", "remark"
            );
            util.exportExcel(response, list, "商标数据");
        }
    }

}
